package cn.edu.swu.jjy;

import java.io.*;
import java.nio.charset.Charset;
//
public class App {
    public static void main(String[] args){
        App app = new App();
        String filePath = "D:\\JAVA\\123.txt";
        try {
            app.readStudentFromFile(filePath);
        }catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void readStudentFromFile(String filePath) throws IOException{
        FileReader fileReader = new FileReader(filePath, Charset.forName("UTF-8"));
        BufferedReader reader = new BufferedReader(fileReader);
        String line = null;

        while ((line=reader.readLine())!=null){
            if(!line.trim().isEmpty()){
                Student student = this.createStudent(line);
                System.out.println(student.toString());
            }
        }

        reader.close();
    }

    private Student createStudent(String line){
        String[] values = line.split("\t");
        Student student = new Student();
        student.setId(values[0]);
        student.setName(values[1]);
        return student;
    }
    public void readStudentFromFile2(String filePath) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(filePath);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
        int data;

        String line = "";
        while((data = inputStreamReader.read()) != -1) {
            if (data == '\n') {
                if (!line.trim().isEmpty()) {
                    Student student = this.createStudent(line);
                    System.out.println(student.toString());
                }

                line = "";
            } else {
                line += (char)data;
            }
        }

        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
    }
}
